<template>
  <div>
    <el-row>
      <el-col :span="2">
        <h5>搜索列表</h5>
      </el-col>
    </el-row>
    <!-- 第一行 -->
    <el-row>
      <el-col :span="5" :offset="3">
        <span>仓库</span>
        <el-select v-model="depotId" placeholder="请选择仓库" style="width: 200px;" clearable>
          <el-option v-for="depot in depotList" :key="depot.id" :label="depot.id" :value="depot.id">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="5">
        <span>制 单 人</span>
        <el-input v-model="input" :disabled="true" style="width: 200px;">
        </el-input>
      </el-col>
      <el-col :span="5">
        <span>产品编号</span>
        <el-select v-model="goodsNumber" placeholder="请选择产品编号" style="width: 200px;" clearable 
        filterable>
          <el-option v-for="item in goodsNumberList" :key="item.goodsNumber" :label="item.goodsNumber" :value="item.goodsNumber">
          </el-option>
        </el-select>
      </el-col>
    </el-row>
    <br>
    <!-- 第二行 -->
    <el-row>
      <el-col :span="5" :offset="3">
        <span>品类</span>
        <el-select v-model="categoryType" placeholder="请选择产品品类" style="width: 200px;" clearable>
          <el-option v-for="item in categoryTypeList" :key="item.type" :label="item.type" :value="item.type">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="5">
        <span>产品物料</span>
        <el-select v-model="meteriType" placeholder="请选择产品物料" style="width: 200px;" @change="change_material()" clearable>
          <el-option v-for="item in meteriTypeList" :key="item.type" :label="item.type" :value="item.type">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="5">
        <span>产品成色</span>
        <el-select v-model="purity" placeholder="请选择产品成色" style="width: 200px;" clearable>
          <el-option v-for="item in purityList" :key="item.purity" :label="item.purity" :value="item.purity">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="2">
        <el-button type="success" size="small" @click="search()">搜索</el-button>
      </el-col>
    </el-row>

    <!-- 添加表格 -->
    <el-row>
      <el-col :span="2">
        <h5>添加列表</h5>
      </el-col>
    </el-row>
    <el-row>
      <template>
        <el-table :data="goodsList" tooltip-effect="dark" style="width: 55%" class="table1">
          <el-table-column label="序号" width="70" align="center" type="index">
          </el-table-column>
          <el-table-column prop="goodsNumber" label="产品编号" width="200" align="center">
          </el-table-column>
          <el-table-column prop="count" label="挑选数量" width="100" align="center">
          </el-table-column>
          <el-table-column prop="depotId" label="仓库编号" width="100" align="center">
          </el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
              <el-button type="success" size="mini" @click="delGoods(scope.$index)">移除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-row>
    <br>

    <!-- 确认添加 -->
    <el-row class="row2">
      <el-col :span="8">
        <span>客户名称</span>
        <el-select v-model="customer" placeholder="请选择客户名称" clearable filterable>
          <el-option v-for="item in customerList" :key="item.id" :label="item.name" :value="item.name">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="5">
        <el-button type="success" @click="addSaleOrder()" size="small">创建</el-button>
      </el-col>
    </el-row>

    <!-- 表格数据 -->
    <el-row>
      <el-col :span="2">
        <h5>产品列表</h5>
      </el-col>
    </el-row>
    <el-row>
      <template>
        <el-table :data="allGoodsList" tooltip-effect="dark" style="width: 93%" class="table2">
          <el-table-column label="序号" width="50" align="center" type="index">
          </el-table-column>
          <el-table-column prop="goods.goodsName" label="产品名称" width="150" align="center">
          </el-table-column>
          <el-table-column prop="goods.goodsNumber" label="产品编号" width="200" align="center">
          </el-table-column>
          <el-table-column prop="goods.categoryType" label="品类" width="80" align="center">
          </el-table-column>
          <el-table-column prop="goods.meteriType" label="物料" width="80" align="center">
          </el-table-column>
          <el-table-column prop="goods.purity" label="成色" width="80" align="center">
          </el-table-column>
          <el-table-column prop="goods.weight" label="重量(g)" width="100" align="center">
          </el-table-column>
          <el-table-column prop="goods.unitPrice" label="单价(g/元)" width="120" align="center">
          </el-table-column>
          <el-table-column label="挑选数量" width="150" align="center">
            <template  slot-scope="scope">
              <el-input-number v-model="count[scope.$index]" @change="handleChange" :min="1" size="mini" placeholder="1">
              </el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
              <!-- @click="handleEdit(scope.row) -->
              <el-button type="success" size="mini" @click="addGoods(scope.row.goods,count[scope.$index],scope.row.depotId)">添加</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-row>

    <!-- 分页 -->
    <el-row>
      <template>
        <div class="block">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="current"
            :page-sizes="[10, 20, 30, 40]" :page-size="size" layout="prev, pager, next, jumper, sizes, total"
            :total="total">
          </el-pagination>
        </div>
      </template>
    </el-row>
  </div>
</template>

<script>
//import Base from "../base/base.vue"
export default {
  data() {
    return {
      // 这里放数据
      current: 1,
      size: 10,
      total: 0,
      depotId: 1,
      categoryTypeList: [],//品类 项链、手镯
      categoryType: "",
      meteriTypeList: [],//物料 黄金、白银
      meteriType: "",
      purityList: [],//成色 
      purity: "",
      depotList: [],//仓库列表
      customerList: [],//客户名称列表
      customer: "",//客户名称
      goodsNumber: "",//产品编号
      input: window.localStorage.getItem("account"),//账号
      baseVO: null,
      goodsList: [],//添加产品列表
      count: [],
      allGoodsList:[],//产品列表
      goodsNumberList: [],//产品编号列表
    }
  },
  methods: {
    addSaleOrder:function(){
      if(this.customer==null || this.customer==""){
        this.$message.error("客户名称不能为空");
        return;
      }
      let data = {
        depotId:this.goodsList[0].depotId,
        customer:this.customer,
        goodsList:this.goodsList
      }
      console.log(data);

      this.$axios.post("saleOrder/add",data).then(res => {
        if (res.data.code == 200) {
          this.$message.success("选货单创建成功!");
          this.goodsList = [];
          this.getCurrentPageData()
        }else{
          this.$message.error("选货单创建失败:" + res.data.$message);
        }
        this.getCurrentPageData()
      })
    },
    findAllConsumer: function () {
      this.$axios.get("businessCompetitor/getName").then(res => {
        this.customerList = res.data.data;
      })
    },
    delGoods: function (index) {
      this.goodsList.splice(index, 1);
    },
    handleChange(value) {
        //console.log(value);
    },
    addGoods:function(oneGoods,number,itDepotId){
      console.log(itDepotId);
      if(typeof number == "undefined"){
        number = 1;
      }

      for(let i=0;i<this.goodsList.length;i++){
        if(this.goodsList[i].goodsNumber == oneGoods.goodsNumber){
          this.goodsList[i].count += number;
          return;
        }

        if(itDepotId != this.goodsList[i].depotId){
          this.$message.error("产品不在同一仓库，请重新选择");
          return;
        }
      }
      let goods = {
        goodsNumber:oneGoods.goodsNumber,
        count:number,
        depotId:itDepotId
      }
      this.goodsList.push(goods);
    },
    //分页函数
    handleSizeChange(val) {
      //页面大小修改时触发
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.getCurrentPageData();
    },
    handleCurrentChange(val) {
      //页码修改时触发
      console.log(`当前页: ${val}`);
      //给当前页属性赋值
      this.current = val;
      //发请求获取数据
      this.getCurrentPageData();
    },
    getCurrentPageData: function () {
      this.search()
    },
    findAllDepot:function(){
      this.$axios.get("depot/listForInStock").then(res=>{
        console.log(res.data.data);
        this.depotList = res.data.data;
      })
    },
    // selectBlur: function (e) {
    //   this.customer = e.target.value
    //   console.log(this.customer);
    // },
    getAllConsumer: function () {
      this.$axios.get("businessCompetitor/getName").then(res => {
        this.userNameList = res.data.data;
      })
    },
    findAllBase: function () {
      this.$axios.get("base/allBase").then(res => {
        this.baseVO = res.data.data;
        this.categoryTypeList = this.baseVO.categories;
        this.meteriTypeList = this.baseVO.materials;
        console.log(this.baseVO);
      })
    },
    change_material: function () {
      // 清空物料
      this.purity = ""
      // 找到当前物料的成色列表
      for (const item of this.baseVO.materials) {
        if (this.meteriType == item.type) {
          this.purityList = item.purities
          return
        }
      }
    },
    findAllGoodsNumber:function(){
      this.$axios.get("goods/findAllGoodsNumber").then(res=>{
        console.log(res.data.data);
        this.goodsNumberList = res.data.data;
      })
    },
    search: function () {
      //准备数据
      let data = {
        current: this.current,
        size: this.size,
        depotId: this.depotId,
        code: this.goodsNumber,
        material: this.meteriType,
        category: this.categoryType,
        purity: this.purity
      }
      console.log(data);
      this.$axios.post("stockManage/searchByCondition", data).then(res => {
        console.log(res.data.data);
        this.allGoodsList = res.data.data.list;
        this.total = res.data.data.total;
        this.size = res.data.data.size;
      })
    }
  },
  mounted: function () {
    this.findAllBase();
    this.getAllConsumer();
    this.findAllDepot();
    this.findAllGoodsNumber();
    this.findAllConsumer();
    this.search();
  }
}
</script>

<style scoped>
.table1{
  width: 100%;
  height: 100%;
  margin: 0 auto;
  border-collapse: collapse;
  border-spacing: 0;
}

.row2{
  margin-left: 400px;
}

.table2{
  margin-left: 50px;
}
</style>